/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * a_InteractiveDoubleList.java
 *
 * Created on 09/09/2010, 02:02:35
 */
package moduledefault.elicitedbases.arff.view;

import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author MASTERUSER
 */
public class InteractiveDoubleList extends javax.swing.JPanel {

    /**
     * list of available columns
     */
    DefaultListModel listModelA = new DefaultListModel();

    /**
     * list of selected columns
     */
    DefaultListModel listModelB = new DefaultListModel();

    public InteractiveDoubleList() {
        initComponents();
    }

    /**
     * sets the list of columns available
     * @param listModel - list of available columns
     */
    public void setListModelA(DefaultListModel listModel) {
        this.listModelA.clear();
        for (int i = 0; i < listModel.size(); i++) {
            this.listModelA.addElement(listModel.get(i));
        }
    }

    /**
     * sets the list of selected columns
     * @param listModel
     */
    public void setListModelB(DefaultListModel listModel) {
        this.listModelB.clear();
        for (int i = 0; i < listModel.size(); i++) {
            this.listModelB.addElement(listModel.get(i));
        }
    }

    /**
     * returns the list of columns available
     * @return listModelA - list of available columns
     */
    public DefaultListModel getListModelA() {
        return listModelA;
    }

    /**
     * returns the list of columns selected
     * @return listModelB - list of selected columns
     */
    public DefaultListModel getListModelB() {
        return listModelB;
    }

    /**
     * add element to list of columns available
     * @param o - columns
     */
    public void addElementListA(Object o) {
        listModelA.addElement(o);
    }

    /**
     * add element to list of columns selected
     * @param o - columns
     */
    public void addElementListB(Object o) {
        listModelB.addElement(o);
    }

    /**
     * removes element from the list of columns available
     * @param index - position of the columns to be removed
     * @return columns removed
     */
    public Object removeElemetListA(int index) {
        return listModelA.remove(index);
    }

    /**
     * removes element from the list of columns selected
     * @param index - position of the columns to be removed
     * @return columns removed
     */
    public Object removeElemetListB(int index) {
        return listModelB.remove(index);
    }

    /**
     * sets the title of the list of columns available
     * @param title
     */
    public void setTitleA(String title){
        jLabelListATitle.setText(title);

    }

    /**
     * sets the title of the list of selected columns
     * @param title
     */
    public void setTitleB(String title){
        jLabelListBTitle.setText(title);
    }

    /**
     * returns the selected columns
     * @return - classes
     */
    public String[] getSelectedClasses(){
        String[] classes = new String[listModelB.size()];
        for (int i= 0; i < listModelB.getSize(); i++) {
            classes[i]= (String)listModelB.get(i);
        }
        return classes;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        jListA = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListB = new javax.swing.JList();
        jButtonParseAToB = new javax.swing.JButton();
        jButtonParseBToA = new javax.swing.JButton();
        jLabelListATitle = new javax.swing.JLabel();
        jLabelListBTitle = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setMinimumSize(new java.awt.Dimension(20, 20));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 130));

        jListA.setModel(listModelA);
        jScrollPane1.setViewportView(jListA);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.weightx = 0.48;
        gridBagConstraints.weighty = 1.0;
        add(jScrollPane1, gridBagConstraints);

        jScrollPane2.setMinimumSize(new java.awt.Dimension(20, 20));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(200, 130));

        jListB.setModel(listModelB);
        jScrollPane2.setViewportView(jListB);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.weightx = 0.48;
        gridBagConstraints.weighty = 1.0;
        add(jScrollPane2, gridBagConstraints);

        jButtonParseAToB.setText(">>");
        jButtonParseAToB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonParseAToBActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 0.04;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
        add(jButtonParseAToB, gridBagConstraints);

        jButtonParseBToA.setText("<<");
        jButtonParseBToA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonParseBToAActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.04;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0);
        add(jButtonParseBToA, gridBagConstraints);

        jLabelListATitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelListATitle.setText("Colunas Disponíveis");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        add(jLabelListATitle, gridBagConstraints);

        jLabelListBTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelListBTitle.setText("Colunas Selecionadas");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        add(jLabelListBTitle, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonParseAToBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonParseAToBActionPerformed
        if (!jListA.isSelectionEmpty()) {
            swapAToB(jListA.getSelectedIndices());
        }
    }//GEN-LAST:event_jButtonParseAToBActionPerformed

    private void jButtonParseBToAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonParseBToAActionPerformed
        // TODO add your handling code here:
        if (!jListB.isSelectionEmpty()) {
            swapBToA(jListB.getSelectedIndices());
        }
    }//GEN-LAST:event_jButtonParseBToAActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonParseAToB;
    private javax.swing.JButton jButtonParseBToA;
    private javax.swing.JLabel jLabelListATitle;
    private javax.swing.JLabel jLabelListBTitle;
    private javax.swing.JList jListA;
    private javax.swing.JList jListB;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    /**
     * move the columns of the list available to the list of selected
     * @param selectedIndices - columns to be moved
     */
    private void swapAToB(int[] selectedIndices) {
        if (selectedIndices != null) {
            Object[] aux = new Object[selectedIndices.length];
            for(int i = selectedIndices.length -1; i>=0;i--){
                aux[i] = listModelA.remove(selectedIndices[i]);
            }
            for(int i =0; i< aux.length;i++){
                listModelB.addElement(aux[i]);
            }
        }
    }

    /**
     * move the columns of the list for the list of disponivies
     * @param selectedIndices - columns to be moved
     */
    private void swapBToA(int[] selectedIndices) {
        if (selectedIndices != null) {
            Object[] aux = new Object[selectedIndices.length];
            for(int i = selectedIndices.length -1; i>=0;i--){
                aux[i] = listModelB.remove(selectedIndices[i]);
            }
            for(int i =0; i< aux.length;i++){
                listModelA.addElement(aux[i]);
            }
        }
    }
}
